package TUILayer;
import java.util.Scanner;
import ControlLayer.*;

/**
 * Write a description of class OrderUI here.
 * 
 * @author (dmab0913 Gruppe4) 
 * @version (a version number or a date)
 */
public class OrderUI
{
    private OrderCtr ordCtr;

    /**
     * Constructor for objects of class OrderUI
     */
    public OrderUI()
    {
        // initialise instance variables
        ordCtr = new OrderCtr();
    }

    public void start()
    {
        OrderMenu();
    }

    private int writeOrderMenu()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\n *** OrdrerMenu***");
        System.out.println("(1) Tilføj ordre");
        System.out.println("(2) Find ordre");
        System.out.println("(3) Opdater ordre");
        System.out.println("(4) List alle ordren");
        System.out.println("(5) List alle ordrens delordrer");
        System.out.println("(6) Return");
        System.out.println("\n Foretag venligst et valg"); 
        int choise = keyboard.nextInt();
        return choise;

    }

    private void OrderMenu()
    {
        boolean exit= false;

        while(!exit)
        {
            int choice = writeOrderMenu();
            if(choice == 1)
            {
                createOrder();
                waitForInput();
            }
            else 
            {
                if( choice == 2)
                {
                    findOrder();
                }
                else 
                {
                    if(choice == 3)
                    {
                        updateOrder();
                    }
                    else 
                    {
                        if(choice == 4)
                        {
                            //                             showOrderInfo();
                            waitForInput();
                        }
                        else 
                        {
                            if( choice == 5)
                            {
                                //                                 showAllOrders();
                                waitForInput();
                            }
                            else
                            {
                                exit=true;
                            }
                        }
                    }
                }
            }
        }
    }

    private void waitForInput()

    {
        Scanner keyboard = new Scanner(System.in);
    }

    private void createOrder()
    {
        ordCtr.createOrder(inputOrderNumber(), inputDate(), inputDeliveryDate(), inputPaymentDeadline(), inputStatus());
    }

    private void findOrder()
    {
        System.out.println(ordCtr.findOrder(inputOrderNumber()));
    }

    //     private void showAllOrders()
    //     {
    //         try
    //         {
    //             ordCtr.printOrder(inputOrderNumber2());
    //         }
    //         catch (Exception e)
    //         {
    //             System.out.println("fejl - prøve igen");
    //         }
    //     }
    // 
    //     private void showOrderInfo()
    //     {
    //         try
    //         {
    //             ordCtr.printOrder(inputOrderNumber());
    //         }
    //         catch (Exception e)
    //         {
    //             System.out.println("fejl - prøve igen");
    //         }
    //     }

    private int updateOrderMenu()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f *** opdater Ordre Menu ***");
        System.out.println(" (1) Ændre datoen");
        System.out.println(" (2) Ændre betalings fristen");
        System.out.println(" (3) Ændre afleveringens datoen");
        System.out.println(" (4) Ændre ordrens status");
        System.out.println(" (5) Return");
        System.out.print("\n fortag et valg: ");

        int choice = keyboard.nextInt();
        return choice;
    }

    private void updateOrder()
    {
        boolean exit = false;
        while(!exit)
        {
            int choice = updateOrderMenu(); 
            if(choice == 1)
            {   
                ordCtr.setDate(inputOrderNumber(), inputDate());
            }
            else
            {
                if(choice == 2)
                {
                    ordCtr.setPaymentDeadline(inputOrderNumber(), inputPaymentDeadline());
                }  
                else
                {
                    if(choice == 3)
                    {
                        ordCtr.setDeliveryDate(inputOrderNumber(), inputDeliveryDate());
                    }  
                    else
                    {
                        if(choice == 4)
                        {
                            ordCtr.setStatus(inputOrderNumber(), inputStatus());
                        }
                        else
                        {
                            exit = true;
                        }//end else
                    }//end else
                }//end else
            }//end else
        }//end else
    }

    private long inputOrderNumber()
    {
        boolean ok = false;
        long code = 0;

        while(!ok)
        {
            //input fra 
            Scanner keyboard = new Scanner(System.in);//Scanner keyboard = new Scanner(System.in); 
            System.out.println(" indtast Ordrens nummer:  ");//System.out.println(" indtast Ordrens nummer:  ");
            try{
                code = keyboard.nextLong();//int code = Integer.parseInt(keyboard.nextLine());
                ok = true;
            }
            catch (Exception e)
            {
                System.out.println("fejl -  prøv igen");
                String input = keyboard.nextLine();
            }
        }//end while
        return code;//return code;
    }

    private long inputOrderNumber2()
    {
        boolean ok = false;
        long code = 0;

        while(!ok)
        {
            //input fra 
            Scanner keyboard = new Scanner(System.in);//Scanner keyboard = new Scanner(System.in); 
            System.out.println(" indtast DelOrdrenrs nummer:  ");//System.out.println(" indtast DelOrdrenrs nummer:  ");
            try{
                code = keyboard.nextInt();//int code = Integer.parseInt(keyboard.nextLine());
                ok = true;
            }
            catch (Exception e)
            {
                System.out.println("fejl -  prøv igen");
                String input = keyboard.nextLine();
            }
        }//end while
        return code;//return code;
    }    

    private int inputDate()
    {
        boolean ok = false;
        int code = 0;

        while(!ok)
        {
            //input fra 
            Scanner keyboard = new Scanner(System.in);//Scanner keyboard = new Scanner(System.in); 
            System.out.println(" indtast Datoen:  ");//System.out.println(" indtast Datoen:  ");
            try{
                code = keyboard.nextInt();//int code = Integer.parseInt(keyboard.nextLine());
                ok = true;
            }
            catch (Exception e)
            {
                System.out.println("fejl -  prøv igen");
                String input = keyboard.nextLine();
            }
        }//end while
        return code;//return code;
    }

    private int inputDeliveryDate()
    {
        boolean ok = false;
        int code = 0;

        while(!ok)
        {
            //input fra 
            Scanner keyboard = new Scanner(System.in);//Scanner keyboard = new Scanner(System.in); 
            System.out.println(" indtast Afleverings datoen:  ");//System.out.println(" indtast Afleverings datoen:  ");
            try{
                code = keyboard.nextInt();//int code = Integer.parseInt(keyboard.nextLine());
                ok = true;
            }
            catch (Exception e)
            {
                System.out.println("fejl -  prøv igen");
                String input = keyboard.nextLine();
            }
        }//end while
        return code;//return code;
    }

    private int inputPaymentDeadline()
    {
        boolean ok = false;
        int code = 0;

        while(!ok)
        {
            //input fra 
            Scanner keyboard = new Scanner(System.in);//Scanner keyboard = new Scanner(System.in); 
            System.out.println(" indtast Betalings fristen:  ");//System.out.println(" indtast Betalings fristen:  ");
            try{
                code = keyboard.nextInt();//int code = Integer.parseInt(keyboard.nextLine());
                ok = true;
            }
            catch (Exception e)
            {
                System.out.println("fejl -  prøv igen");
                String input = keyboard.nextLine();
            }
        }//end while
        return code;//return code;
    }

    private String inputStatus()
    {
        // makes an object keyboard to read input from the console
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" indtast Ordrens status: ");
        String status = keyboard.nextLine();
        return status;
    }
}
